import scrapy
from lianjia.items import LianjiaItem


class LianjiaHouseSpider(scrapy.Spider):
    name = 'lianjia_house'
    allowed_domains = ['lianjia.com']
    page = 1
    start_urls = ['https://cd.lianjia.com/zufang/pg1/']

    def parse(self, response):
        li_list = response.xpath("//*[@id='content']/div[1]/div[1]/div[*]/a/@href").extract()
        for li in li_list:
            details_url = li
            if details_url is not None:
                details_url = "https://cd.lianjia.com" + details_url
                yield scrapy.Request(url=details_url, callback=self.details_parse)
            # 获取下一页
        self.page += 1
        next_page = "https://cd.lianjia.com/zufang/pg{}/#contentList".format(self.page)
        if self.page < 1:
            yield scrapy.Request(next_page, callback=self.parse)
        else:
            print("结束了")

    def details_parse(self, response):
        title = response.xpath("/html/body/div[3]/div[1]/div[3]/p/text()").get()
        data_time = response.xpath("/html/body/div[3]/div[1]/div[3]/div[1]/text()").get()
        price = response.xpath("//*[@id='aside']/div[1]/span").get()
        lease = response.xpath("//*[@id='aside']/ul/li[1]/text()").get()
        housing_types = response.xpath("//*[@id='aside']/ul/li[2]/text()").get()
        floor = response.xpath("//*[@id='aside']/ul/li[3]/span[2]").get()
        area = response.xpath('//*[@id="info"]/ul[1]/li[2]').get()
        toward = response.xpath('//*[@id="info"]/ul[1]/li[3]').get()
        check_time = response.xpath('//*[@id="info"]/ul[1]/li[6]').get()
        floor_num = response.xpath('//*[@id="info"]/ul[1]/li[8]').get()
        elevator = response.xpath('//*[@id="info"]/ul[1]/li[9]').get()
        water = response.xpath('//*[@id="info"]/ul[1]/li[12]').get()
        electricity = response.xpath('//*[@id="info"]/ul[1]/li[14]').get()
        fuel_gas = response.xpath('//*[@id="info"]/ul[1]/li[15]').get()
        lease_data = response.xpath('//*[@id="info"]/ul[2]/li[2]').get()
        item = LianjiaItem()
        # 标题
        item['title'] = title
        # 发布时间
        item['data_time'] = data_time
        # 价格
        item['price'] = price
        # 租赁方式  整租合租
        item['lease'] = lease
        # 房屋类型
        item['housing_types'] = housing_types
        # 楼层
        item['floor'] = floor
        # 面积
        item['area'] = area
        # 朝向
        item['toward'] = toward
        # 入住时间
        item['check_time'] = check_time
        # 楼层
        item['floor_num'] = floor_num
        # 有无电梯
        item['elevator'] = elevator
        # 用水类型
        item['water'] = water
        # 用电类型
        item['electricity'] = electricity
        #燃气
        item['fuel_gas'] = fuel_gas
        #租期时间
        item['lease_data'] = lease_data

        # yield item
